<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category  Zend
 * @package   Zend_DataGrid
 * @subpackage Column
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 *
 */

/** @see Zend_DataGrid_Column */
require_once 'Zend/DataGrid/Column.php';



/**
 * Zend_DataGrid_Column_Button
 *
 * The button column contains a user-defined command button, such as Add or Remove, that corresponds with each row in the column.
 *
 * The caption of the buttons in the column is determined by {@link getText} and {@link getDataField}. If both are present,
 * the latter takes precedence. The DataField property refers to the name of the field in datasource whose value will be
 * used as the button caption. If DataTextFormat is not empty, the value will be formatted before rendering.
 *
 * The buttons in the column can be set to display as hyperlinks or push buttons by setting the ButtonType property.
 * The CommandName will assign its value to all button's CommandName property. The datagrid will capture the command
 * event where you can write event handlers based on different command names.
 *
 * @category   Zend
 * @package    Zend_DataGrid
 * @subpackage Column
 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd     New BSD License
 * @version   $Id: Button.php 6137 2008-09-13 17:37:27Z azoungrana $
 */
class Zend_DataGrid_Column_Button extends Zend_DataGrid_Column
{
    /**#@+
     * The type of editing button
     */
    const BUTTON_HYPERLINK = 'hyperlink';
    const BUTTON_PUSH = 'push';
    const BUTTON_IMAGE = 'image';
    /**#@-*/

    /**
     * The button text
     *
     * @var string
     */
    protected $_buttonText = null;

    /**
     * The button type
     *
     * @var string
     */
    protected $_buttonType = null;

    /**
     * The command name
     *
     * @var string
     */
    protected $_commandName = null;

    /**
     * The data image url field
     *
     * @var array
     */
    protected $_dataImageUrlFields = array();

    /**
     * The data image url format
     *
     * @var string
     */
    protected $_dataImageUrlFormat = null;

    /**
     * The url of image
     *
     * @var string
     */
    protected $_imageUrl = null;



    /**
     * Sets the type of command button.
     *
     * @param string $value
     * @return Zend_DataGrid_Column_Button
     * @throws  Zend_DataGrid_Column_Exception for invalide button type
     */
    public function setButtonType($value)
    {
        $buttonTypes = array(self::BUTTON_HYPERLINK, self::BUTTON_IMAGE, self::BUTTON_PUSH);
        if (in_array($value, $buttonTypes))
            $this->_buttonType = $value;
        else
            throw new Zend_DataGrid_Column_Exception('The button type must take its value in :'. implode(' ,', $buttonTypes));

        return $this;
    }

    /**
     * Gets the type of command button. Defaults to self::BUTTON_HYPERLINK
     *
     * @return string
     */
    public function getButtonType()
    {
        if (null === $this->_buttonType) {
            $this->_buttonType = self::BUTTON_HYPERLINK;
        }

        return $this->_buttonType;
    }

    /**
     * Sets the command name associated with the Command event
     *
     * @param string $value
     * @return Zend_DataGrid_Column_Button
     */
    public function setCommandName($value)
    {
        $this->_commandName = (string) $value;
        return $this;
    }

    /**
     * Gets the command name associated with the Command event
     *
     * @return string
     */
    public function getCommandName()
    {
        return $this->_commandName;
    }

    /**
     * Sets the field name from the data source to bind to the button image url
     *
     * @param string $value
     * @return Zend_DataGrid_Column_Button
     */
    public function setDataImageUrlField($value)
    {
        $this->clearDataImageUrlFields();
        $this->_dataImageUrlFields[] = (string) $value;

        return $this;
    }

    /**
     * Return the fields names from the data source to bind to the button image url
     *
     * @return array
     */
    public function getDataImageUrlFields()
    {
        return $this->_dataImageUrlFields;
    }

    /**
     * Add a field name from the data source to bind to the button image url
     *
     * @param string $value
     * @return Zend_DataGrid_Column_Button
     */
    public function addDataImageUrlField($value)
    {
        $this->_dataImageUrlFields[] = (string) $value;
    }

    /**
     * Add fields names from the data source to bind to the button image url
     *
     * @param array $values
     * @return Zend_DataGrid_Column_Button
     */
    public function addDataImageUrlFields(array $values)
    {
        foreach ($values as $field)
            $this->addDataImageUrlField($field);

        return $this;
    }


    /**Clear the image url data filds
     *
     * @return Zend_DataGrid_Column_Button
     */
    public function clearDataImageUrlFields()
    {
        $this->_dataImageUrlFields = array();
        return $this;
    }

    /**
     * Sets the formatting string used to control how the button image url will be displayed.
     *
     * @param string $value
     * @return Zend_DataGrid_Column_Button
     */
    public function setDataImageUrlFormat($value)
    {
        $this->_dataImageUrlFormat = (string) $value;
        return $this;
    }

    /**
     * Gets the formatting string used to control how the button image url will be displayed.
     *
     * @return string
     */
    public function getDataImageUrlFormat()
    {
        return $this->_dataImageUrlFormat;
    }

    /**
     * Sets the URL of the image file for image buttons
     *
     * @param string $value
     * @return Zend_DataGrid_Column_Button
     */
    public function setImageUrl($value)
    {
        $this->_imageUrl = (string) $value;
        return $this;
    }

    /**
     * Gets the URL of the image file for image buttons
     *
     * @return string
     */
    public function getImageUrl()
    {
        return $this->_imageUrl;
    }

    /**
     * Sets the text caption of the button
     *
     * @param string $value
     * @return Zend_DataGrid_Column_Button
     */
    public function setText($value)
    {
        $this->_buttonText = (string) $value;
        return $this;
    }

    /**
     * Gets the text caption of the button
     *
     * @return string
     */
    public function getText()
    {
        return $this->_buttonText;
    }

}
